package com.hongning.railway.ui.main.mine.partcheck;

import android.app.Application;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableField;
import android.databinding.ObservableList;
import android.support.annotation.NonNull;

import com.hongning.railway.R;
import com.hongning.railway.data.AppRepository;
import com.hongning.railway.entity.resbean.ResKaoHeBean;
import com.hongning.railway.ui.main.mine.partcheck.itemviewmodel.PartCheckDetailNormalItemViewModel;
import com.hongning.railway.ui.main.mine.partcheck.itemviewmodel.PartCheckDetailSectionItemViewModel;
import com.hongning.railway.util.GsonUtil;

import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.tatarka.bindingcollectionadapter2.BR;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.OnItemBind;

public class PartyCheckDetailFragmentViewModel extends BaseViewModel<AppRepository> {
    public ItemBinding<MultiItemViewModel> itemBinding = ItemBinding.of(new OnItemBind<MultiItemViewModel>() {
        @Override
        public void onItemBind(ItemBinding itemBinding, int position, MultiItemViewModel item) {
            if (1 == (int)item.getItemType()){
                itemBinding.set(BR.viewModel, R.layout.item_party_detail_section);
            }else {
                itemBinding.set(BR.viewModel, R.layout.item_party_detail_normal);
            }
        }
    });
    public ObservableList<MultiItemViewModel> items = new ObservableArrayList<>();
    public ObservableField<String> score = new ObservableField<>();
    public ObservableField<String> name = new ObservableField<>();
    public PartyCheckDetailFragmentViewModel(@NonNull Application application) {
        super(application);
    }


    public PartyCheckDetailFragmentViewModel(@NonNull Application application, AppRepository model) {
        super(application, model);
    }

    public void setData(ResKaoHeBean mResKaoHeBean){
        score.set(mResKaoHeBean.getTotleScore()+"");
        name.set(mResKaoHeBean.getGroupName());
        List<ResKaoHeBean.DataBean> result=  GsonUtil.getObjectList(mResKaoHeBean.getDetails(),ResKaoHeBean.DataBean.class);
        for (ResKaoHeBean.DataBean data:result) {
            items.add(new PartCheckDetailSectionItemViewModel(this,data));
            if (data.getChildren()!=null&&!data.getChildren().isEmpty()){
                List<ResKaoHeBean.DataBean.ChildrenBean> datas = data.getChildren();
                for (ResKaoHeBean.DataBean.ChildrenBean res:datas) {
                    items.add(new PartCheckDetailNormalItemViewModel(this,res));
                }
            }
        }
    }
}
